/* ****************************************************************************
atscene.h
-------------------------------------------------------------------------------

Copyright (c) 2017, Tain L.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

**************************************************************************** */

#if !defined(SPEARS_ASSET_ATSCENE_H)
#define SPEARS_ASSET_ATSCENE_H

#include "my-prerequisites.h"

#include "atmesh.h"
#include "atlight.h"
#include "atnode.h"
#include "atmaterial.h"

namespace Spears
{
	namespace Asset
	{
		class atScene
		{
		public:
			atScene();
			virtual ~atScene();
			
		public:
			/**
			If this method succeeded, the pointer will be set to nullptr,
			which means this scene is in charge of the mesh.
			The mesh will be released by this scene.
			*/
			void manage_mesh(atMesh*& p_mesh);

			/**
			If this method succeeded, the pointer will be set to nullptr,
			which means this scene is in charge of the light.
			The light will be released by this scene.
			*/
			void manage_light(atLight*& p_light);

			/**
			If this method succeeded, the pointer will be set to nullptr,
			which means this scene is in charge of the tree.
			The tree will be released by this scene.
			*/
			void manage_scene_tree(atNode*& p_root);

			/**
			If this method succeeded, the pointer will be set to nullptr,
			which means this scene is in charge of the material.
			The tree will be released by this scene.
			*/
			void manage_material(atMaterial*& p_material);

			Spears::index find_light(const std::string& name);

		public:
			std::vector<atMesh*>& meshes();
			std::vector<atLight*>& lights();
			const std::vector<atMaterial*>& materials() const;

			const std::vector<atMesh*>& meshes() const;

			atNode const* scene_tree() const;

		protected:
			void release_scene_tree(atNode* p_node);

		private:
			std::vector<atMesh*>		m_meshes;
			std::vector<atLight*>		m_lights;
			std::vector<atMaterial*>	m_materials;
			atNode*						m_root;
		};

		inline atScene::atScene()
			: m_root(nullptr)
		{}

		inline atScene::~atScene()
		{
			for (auto& item : m_meshes)
			{
				if(nullptr != item)
					delete item;
			}

			for (auto& item : m_lights)
			{
				if (nullptr != item)
					delete item;
			}

			release_scene_tree(m_root);
		}

		inline void atScene::release_scene_tree(atNode* p_node)
		{
			if (nullptr != p_node)
			{
				for(auto& child : p_node->m_children)				
				{	
					release_scene_tree(child);
				}

				safe_delete(p_node);
			}
		}

		inline void atScene::manage_mesh(atMesh*& p_mesh)
		{
			m_meshes.push_back(p_mesh);
			p_mesh = nullptr;
		}

		inline void atScene::manage_light(atLight*& p_light)
		{
			m_lights.push_back(p_light);
			p_light = nullptr;
		}

		inline void atScene::manage_scene_tree(atNode*& p_root)
		{
			m_root = p_root;
			p_root = nullptr;
		}

		inline void atScene::manage_material(atMaterial*& p_material)
		{
			m_materials.push_back(p_material);
			p_material = nullptr;
		}

		inline Spears::index atScene::find_light(const std::string& name)
		{
			for (index i = 0; i < m_lights.size(); ++i)
			{
				if (m_lights[i]->m_name == name)
				{
					return i;
				}
			}

			return INVALID_INDEX;
		}

		inline std::vector<atMesh*>& atScene::meshes()
		{
			return m_meshes;
		}

		inline std::vector<atLight*>& atScene::lights()
		{
			return m_lights;
		}

		inline const std::vector<atMaterial*>& atScene::materials() const
		{
			return m_materials;
		}

		inline const std::vector<atMesh*>& atScene::meshes() const
		{
			return m_meshes;
		}

		inline atNode const* atScene::scene_tree() const
		{
			return m_root;
		}
	} // namespace Asset
} //namespace Spears

#endif // SPEARS_ASSET_ATSCENE_H